package com.act.components;

import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpUtil;
import com.act.constant.LoginTypeEnums;
import com.act.constant.UserRedisKeyConstants;
import com.act.constant.UserTypeEnums;
import com.act.moudles.user.entity.Menu;
import com.act.moudles.user.entity.User;
import com.act.moudles.user.service.ComboService;
import com.act.moudles.user.service.UserService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 用户组件
 * @author: mengxiangzhong
 * @date: 2023/2/3 9:47
 * @TODO:
 **/
@Component
public class UserComponent implements StpInterface {


    @Resource
    private UserService userService;

    @Resource
    private ComboService comboService;

    /**
     * 缓存用户基本信息
     * @param user
     */
    public  void  setUserBasicSession(User user,String systemFlag){
        StpUtil.getSession().set(UserRedisKeyConstants.userBasic+":"+systemFlag,user);
    }

    /**
     * 获取到当前的操作用户的真实姓名(不支持异步线程)
     * @return
     */
    public String getCurrentRealName(String systemFlag){
        User  user = (User) StpUtil.getSession().get(UserRedisKeyConstants.userBasic+":"+systemFlag);
        return user.getLoginName();
    }

    public Map<Long,String>  getUserName(List<Long> userIdList){
        Map<Long,User>  userMap = userService.findUserByIds(userIdList);
        Map<Long,String>  userNameMap = new HashMap<>();
        for(Map.Entry<Long,User> e : userMap.entrySet()){
            userNameMap.put(e.getKey(),e.getValue().getRealName());
        }
        return userNameMap;
    }

    /**
     * 获取到当前用户
     * @return
     */
    public User getCurrentUser(String systemFlag){
        User  user = (User) StpUtil.getSession().get(UserRedisKeyConstants.userBasic+":"+systemFlag);
        return user;
    }

    /**
     * 获取到当前用户Id
     * @return
     */
    public Long getUserId(String systemFlag){
        User user = (User) StpUtil.getSession().get(UserRedisKeyConstants.userBasic+":"+systemFlag);
        return user.getUserId();
    }

    /**
     * 获取到当前用户uId
     * @param userId
     * @return
     */
    public User getUserById(Long userId){
        return userService.getById(userId);
    }

    /**
     * 获取到用户的默认文件地址
     * @return
     */
    public String getUserBasicPath(){
        return userService.basicFilePath(getCurrentUser(LoginTypeEnums.WEB.getType()));
    }

    /**
     * 获取到多用户uId
     * @return
     */
    public String getUserTenAntId(){
        return getCurrentUser(LoginTypeEnums.WEB.getType()).getTenantId();
    }

    public User getUserByLoginName(String loginName){
        return userService.getByUseNameOrPhone(loginName,null);
    }


    @Override
    public List<String> getPermissionList(Object o, String s) {
        User user =   userService.getById(Long.parseLong(o.toString()));
        List<Menu>  menuList = comboService.listComboDetail(user.getComboId());
        return menuList.stream().map(Menu::getPerms).collect(Collectors.toList());
    }

    @Override
    public List<String> getRoleList(Object o, String s) {
         User user =   userService.getById(Long.parseLong(o.toString()));
         String userTypeName = UserTypeEnums.getName(user.getUserType());
         return Arrays.asList(userTypeName);
    }


}
